package com.sright.bcard.enterprise.web.controller;

import com.sright.bcard.common.service.share.dto.enterprise.MaterialDTO;
import com.sright.bcard.common.service.share.enums.MaterialTypeEnum;
import com.sright.bcard.enterprise.share.service.MaterialService;
import com.sright.bcard.common.service.share.mvcresolver.EnableLoginAuth;
import com.sright.common.response.SrightResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 名片背景视频列表
 */
@RestController
@RequestMapping("/media")
public class MediaController extends BaseController{

    @Autowired
    private MaterialService materialService;

    /**
     * 获取名片背景视频列表
     * @return
     */
    @GetMapping("/media.json")
    @EnableLoginAuth
    public SrightResponse mediaList() {
        MaterialDTO materialDTO = new MaterialDTO();
        materialDTO.setType(MaterialTypeEnum.CARD_BACKGROUND_VEDIO.getValue());
        buildLoginUser(materialDTO);
        return materialService.list(materialDTO);
    }

    /**
     * 新增背景视频
     * @return
     */
    @PostMapping("/media.json")
    @EnableLoginAuth
    public SrightResponse addMedia(@RequestBody MaterialDTO materialDTO) {
        buildLoginUser(materialDTO);
        materialDTO.setType(MaterialTypeEnum.CARD_BACKGROUND_VEDIO.getValue());
        return materialService.addMaterial(materialDTO);
    }

    /**
     * 修改背景视频
     * @return
     */
    @PutMapping("/media.json")
    @EnableLoginAuth
    public SrightResponse updateMedia(@RequestBody MaterialDTO materialDTO) {
        buildLoginUser(materialDTO);
        materialDTO.setType(MaterialTypeEnum.CARD_BACKGROUND_VEDIO.getValue());
        return materialService.editMaterial(materialDTO);
    }

    /**
     * 删除背景视频
     * @param materialDTO
     * @return
     */
    @DeleteMapping("/media.json")
    @EnableLoginAuth
    public SrightResponse deleteMedia(@RequestBody MaterialDTO materialDTO) {
        return SrightResponse.defaultSuccess(materialService.deleteMaterial(materialDTO.getId()));
    }

    /**
     * 构建登录用户信息
     * @param materialDTO
     */
    private void buildLoginUser(MaterialDTO materialDTO) {
        materialDTO.setUserId(userContext.getUserId());
        materialDTO.setToken(userContext.getToken());
        materialDTO.setEnterpriseId(userContext.getEnterpriseId());
    }

}
